import { Component, OnInit, ViewChild } from '@angular/core';
import { STColumn, STComponent, STData } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema, SFUISchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { DownloadService } from '../../services/download.service';

@Component({
  selector: 'app-download-center-components-list',
  templateUrl: './list.component.html'
})
export class DownloadComponentsListComponent implements OnInit {
  ui: SFUISchema = {};
  schema: SFSchema = {};
  columns: STColumn[] = [];
  @ViewChild('st', { static: false }) st!: STComponent;
  @ViewChild('sf', { static: false }) sf!: SFComponent;
  constructor(public service: DownloadService, private modal: NzModalService) {}

  /**
   * 查询参数
   */
  get reqParams() {
    const params = Object.assign({}, this.sf?.value || {});
    delete params._$expand;
    return { ...params, createTime: params?.createTime?.start, applyEndTime: params?.createTime?.end };
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
  }
  /**
   * 程序初始化入口
   */
  ngOnInit() {
    this.initSF();
    this.initST();
  }

  /**
   * 初始化查询表单
   */
  initSF() {
    this.schema = {
      properties: {
        _$expand: {
          type: 'boolean',
          ui: { hidden: true }
        },
        createTime: {
          type: 'string',
          title: '创建时间',
          ui: {
            widget: 'sl-from-to',
            type: 'date',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        }
      }
    };
    this.ui = { '*': { spanLabelFixed: 80, grid: { span: 8, gutter: 4 } } };
  }

  /**
   * 初始化数据列表
   */
  initST() {
    this.columns = [
      { title: '文件名称', index: 'dataSourcesDetail', className: 'text-center' },
      { title: '文件来源', index: 'dataSources', className: 'text-center' },
      { title: '文件大小', index: 'dataSize', width: '120px', className: 'text-center' },
      {
        title: '生成状态',
        index: 'status',
        width: '120px',
        className: 'text-center',
        type: 'enum',
        enum: {
          0: '生成中',
          1: '已完成',
          2: '失败'
        }
      },
      { title: '下载次数', index: 'downloadCount', width: '120px', className: 'text-center' },
      { title: '创建时间', index: 'createTime', width: '120px', className: 'text-center' },
      { title: '生成时间', index: 'completeTime', width: '180px', className: 'text-center' },
      {
        title: '操作',
        fixed: 'right',
        width: '170px',
        className: 'text-center',
        buttons: [{ text: '下载', click: _record => this.download(_record) }]
      }
    ];
  }

  /**
   * 删除单个实例
   * @param record 记录实例
   */
  delOne(record: STData) {
    const headers = [{ key: 'Content-Type', value: 'application/json' }];
    this.modal.confirm({
      nzTitle: '<i>删除确认</i>',
      nzContent: `<b>即将删除 当前行数据,请仔细核对,避免误操作！<br>是否删除？</br>`,
      nzOnOk: () =>
        this.service.request(this.service.encodeUrlHeader(this.service.$api_del_many, headers), { fileKey: record.id }).subscribe(res => {
          if (res) {
            this.service.msgSrv.success('数据删除成功！');
            this.st?.reload();
          }
        })
    });
  }
  download(record: STData) {
    const headers = [{ key: 'Content-Type', value: 'application/json' }];
    this.service.downloadFile(
      this.service.encodeUrlHeader(this.service.$api_download_file, headers),
      {},
      { fileKey: record.fileUniqueKey },
      'POST'
    );
  }
}
